package cn.well.cloud.generator.template.config;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.sun.javafx.PlatformUtil;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

/**
 * ADI项目模板生成 引擎
 *
 * @author fengshuonan
 * @date 2017-05-07 22:15
 */
public abstract class GunsTemplateEngine extends AbstractTemplateEngine {

    private GroupTemplate groupTemplate;

    public GunsTemplateEngine() {
        initBeetlEngine();
    }

    protected void initBeetlEngine() {
        Properties properties = new Properties();
        properties.put("RESOURCE.root", "");
        properties.put("DELIMITER_STATEMENT_START", "<%");
        properties.put("DELIMITER_STATEMENT_END", "%>");
        properties.put("HTML_TAG_FLAG", "##");
        Configuration cfg = null;
        try {
            cfg = new Configuration(properties);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
        groupTemplate = new GroupTemplate(resourceLoader, cfg);
        groupTemplate.registerFunctionPackage("tool", new ToolUtil());
    }

    protected void configTemplate(Template template) {
        template.binding("controller", super.controllerConfig);
        template.binding("context", super.contextConfig);
        template.binding("dao", super.daoConfig);
        template.binding("service", super.serviceConfig);
        template.binding("sqls", super.sqlConfig);
        template.binding("table", super.tableInfo);
    }

    protected void generateFile(String template, String filePath) {
        Template pageTemplate = groupTemplate.getTemplate(template);
        configTemplate(pageTemplate);
        if (PlatformUtil.isWindows()) {
            filePath = filePath.replaceAll("/+|\\\\+", "\\\\");
        } else {
            filePath = filePath.replaceAll("/+|\\\\+", "/");
        }
        File file = new File(filePath);
        File parentFile = file.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            pageTemplate.renderTo(fileOutputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void restStart() {
        //配置之间的相互依赖
        super.initConfig();
            String extXmlPath = StrUtil.format(super.getContextConfig().getProjectPath() + super.daoConfig.getXmlExtPathTemplate(),
                    StrUtil.upperFirst(super.getContextConfig().getBizEnName()));
            File extXmlFile = FileUtil.file(extXmlPath);
            if(!extXmlFile.exists()){
                generateXmlExt();
            }else{
                System.out.println("mapping/ext/"+super.getContextConfig().getBizEnName()+"Mapper.xml已经存在，若需要重新生成，请先手动删除");
            }
    }
    
    
    public void webStart() {
        //配置之间的相互依赖
        super.initConfig();
        //生成模板

             String controllerPath = StrUtil.format(super.getContextConfig().getProjectPath() + super.getControllerConfig().getControllerPathTemplate(),
                     StrUtil.upperFirst(super.getContextConfig().getBizEnName()));
            File controllerFile = FileUtil.file(controllerPath);
            //如果文件已经存在，则不重复生成
            if(!controllerFile.exists()){
                generateController();
            }else{
                System.out.println(super.contextConfig.getEntityName()+"Controller.java控制器已经存在，若需要重新生成，请先手动删除");
            }

            String htmlPath = StrUtil.format(super.getContextConfig().getProjectPath() + getPageConfig().getPagePathTemplate(),
                    super.getContextConfig().getBizEnName(), super.getContextConfig().getBizEnName());
            File htmlFile = FileUtil.file(htmlPath);
            if(!htmlFile.exists()){
             generatePageHtml();
            }else{
                System.out.println(super.getContextConfig().getBizEnName()+".html已经存在，若需要重新生成，请先手动删除");
            }
        
            String addHtmlPath = StrUtil.format(super.getContextConfig().getProjectPath() + getPageConfig().getPageAddPathTemplate(),
                    super.getContextConfig().getBizEnName(), super.getContextConfig().getBizEnName());
            File addHtmlFile = FileUtil.file(addHtmlPath);
            if(!addHtmlFile.exists()){
                generatePageAddHtml();
            }else{
                System.out.println(super.getContextConfig().getBizEnName()+"_add.html已经存在，若需要重新生成，请先手动删除");
            }
      
            String editHtmlPath = StrUtil.format(super.getContextConfig().getProjectPath() + getPageConfig().getPageEditPathTemplate(),
                    super.getContextConfig().getBizEnName(), super.getContextConfig().getBizEnName());
            File editHtmlFile = FileUtil.file(editHtmlPath);
            if(!editHtmlFile.exists()){
                generatePageEditHtml();
            }else{
                System.out.println(super.getContextConfig().getBizEnName()+"_edit.html已经存在，若需要重新生成，请先手动删除");
            }

            String jsPath = StrUtil.format(super.getContextConfig().getProjectPath() + getPageConfig().getPageJsPathTemplate(),
                    super.getContextConfig().getBizEnName(), super.getContextConfig().getBizEnName());
            File jsFile = FileUtil.file(jsPath);
            if(!jsFile.exists()){
                generatePageJs();
            }else{
                System.out.println(super.getContextConfig().getBizEnName()+".js已经存在，若需要重新生成，请先手动删除");
            }

            String addJsPath = StrUtil.format(super.getContextConfig().getProjectPath() + getPageConfig().getPageAddJsPathTemplate(),
                    super.getContextConfig().getBizEnName(), super.getContextConfig().getBizEnName());
            File addJsFile = FileUtil.file(addJsPath);
            if(!addJsFile.exists()){
                generatePageAddJs();
            }else{
                System.out.println(super.getContextConfig().getBizEnName()+"_add.js已经存在，若需要重新生成，请先手动删除");
            }

            String editJsPath = StrUtil.format(super.getContextConfig().getProjectPath() + getPageConfig().getPageEditJsPathTemplate(),
                    super.getContextConfig().getBizEnName(), super.getContextConfig().getBizEnName());
            File editJsFile = FileUtil.file(editJsPath);
            if(!editJsFile.exists()){
                generatePageEditJs();
            }else{
                System.out.println(super.getContextConfig().getBizEnName()+"_edit.js已经存在，若需要重新生成，请先手动删除");
            }

            String extXmlPath = StrUtil.format(super.getContextConfig().getProjectPath() + super.daoConfig.getXmlExtPathTemplate(),
                    StrUtil.upperFirst(super.getContextConfig().getBizEnName()));
            File extXmlFile = FileUtil.file(extXmlPath);
            if(!extXmlFile.exists()){
                generateXmlExt();
            }else{
                System.out.println(super.getContextConfig().getBizEnName()+"ext/xml已经存在，若需要重新生成，请先手动删除");
            }
           
        
    }

    protected abstract void generatePageEditHtml();

    protected abstract void generatePageAddHtml();

    protected abstract void generatePageAddJs();
    
    protected abstract void generatePageEditJs();

    protected abstract void generatePageJs();

    protected abstract void generatePageHtml();

    protected abstract void generateController();

    protected abstract void generateSqls();
    
    protected abstract void generateXmlExt();
}
